package com.xd.spring.liteflow.controller;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.RandomUtil;
import com.xd.spring.liteflow.entity.UserInfo;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/first")
public class FirstController {

    @Resource
    private FlowExecutor flowExecutor;

    @RequestMapping("/chain1")
    public Object chain1() {
        Map<String, Object> r = new HashMap<>();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();


        try {
            LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg");

            r.put("code", response.getCode());
            r.put("message", response.getMessage());
            r.put("success", response.isSuccess());
            r.put("requestId", response.getRequestId());

            if (Objects.equals(response.isSuccess(), false)) {
                Exception exception = response.getCause();
                r.put("exception", exception.getMessage());
            }

        } finally {
            stopWatch.stop();
            r.put("cost", stopWatch.prettyPrint());
        }

        return r;


    }

    @RequestMapping("/chain2")
    public Object chain2() {
        Map<String, Object> r = new HashMap<>();
        long start = System.currentTimeMillis();


        LiteflowResponse response = flowExecutor.execute2Resp("chain2", "arg");


        long end = System.currentTimeMillis();
        long cost = (end - start) / 1000;
        r.put("code", response.getCode());
        r.put("message", response.getMessage());
        r.put("success", response.isSuccess());
        r.put("requestId", response.getRequestId());
        r.put("cost", cost);
        return r;
    }

    @RequestMapping("/chain3")
    public Object chain3() {
        Map<String, Object> r = new HashMap<>();
        long start = System.currentTimeMillis();


        LiteflowResponse response = flowExecutor.execute2Resp("chain3", "arg");


        long end = System.currentTimeMillis();
        long cost = (end - start) / 1000;
        r.put("code", response.getCode());
        r.put("message", response.getMessage());
        r.put("success", response.isSuccess());
        r.put("requestId", response.getRequestId());
        r.put("cost", cost);
        return r;
    }


    @RequestMapping("/chain4")
    public Object chain4() {
        Map<String, Object> r = new HashMap<>();
        long start = System.currentTimeMillis();


        LiteflowResponse response = flowExecutor.execute2Resp("chain4", "arg");


        long end = System.currentTimeMillis();
        long cost = (end - start) / 1000;
        r.put("code", response.getCode());
        r.put("message", response.getMessage());
        r.put("success", response.isSuccess());
        r.put("requestId", response.getRequestId());
        r.put("cost", cost);
        return r;
    }

    @RequestMapping("/chain5")
    public Object chain5() {
        Map<String, Object> r = new HashMap<>();
        long start = System.currentTimeMillis();


        LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");


        long end = System.currentTimeMillis();
        long cost = (end - start) / 1000;
        r.put("code", response.getCode());
        r.put("message", response.getMessage());
        r.put("success", response.isSuccess());
        r.put("requestId", response.getRequestId());
        r.put("cost", cost);
        return r;
    }


    @RequestMapping("/chain6")
    public Object chain6() {
        Map<String, Object> r = new HashMap<>();
        long start = System.currentTimeMillis();


        long id = RandomUtil.randomLong(10000L, 100000L);

        LiteflowResponse response = flowExecutor.execute2Resp("chain6", id, UserInfo.class);

        UserInfo userInfo = response.getContextBean(UserInfo.class);


        long end = System.currentTimeMillis();
        long cost = (end - start) / 1000;
        r.put("code", response.getCode());
        r.put("message", response.getMessage());
        r.put("success", response.isSuccess());
        r.put("requestId", response.getRequestId());
        r.put("cost", cost);
        r.put("data", userInfo);
        return r;
    }
}
